#!/usr/bin/env python
# -*- coding: utf-8 -*-

# from io import BytesIO
# import requests as req
# import base64


# import logging  # 引入logging模块
# import csv
# import time
# import configparser
# import os
# import sys

from functools import wraps


def singleton(cls):
    instances = {}

    @wraps(cls)
    def get_instance(*args, **kwargs):
        if cls not in instances:
            instances[cls] = cls(*args, **kwargs)
        return instances[cls]
    return get_instance


# def get_arg(option):
#     # print(sys.argv)
#     args = sys.argv[1:]
#     for index, arg in enumerate(args):
#         if arg == option:
#             if len(args) > index+1:
#                 return args[index+1]
#     return None


# def get_config(name=None):
#     if not name:
#         name = get_arg('-c')

#     if not name:
#         return {}

#     config = configparser.ConfigParser()
#     config.read(name)
#     # print(cfg)

#     # print( config.sections() )

#     return dict((section, dict(config.items(section))
#                  )for section in config.sections())


# def set_config(name, data):
#     # print('set_config,', name, data)
#     config = configparser.ConfigParser()

#     config.add_section("options")

#     for key, value in data.items():
#         config.set("options", key,  str(value))

#     with open(name, "w") as f:
#         config.write(f)


# def get_csv_data(name=None):

#     # if not name:
#     #     name = get_arg('--data')

#     if not name:
#         return []

#     def fn(row, fields):
#         # print(',,,', row, meta)
#         ret = dict((fld, row[index]) for index, fld in enumerate(fields))
#         return ret

#     with open(name, encoding="utf-8") as csvfile:
#         rows = csv.reader(csvfile)
#         recs = [row for row in rows]
#         meta = recs[0]
#         print('csv, cn:', recs[0])
#         # print('csv, en:', recs[1])

#         ret = [fn(rec, meta) for rec in recs[1:]]
#         return ret


# def set_csv_data(name, data):
#     print('set_csv_data,', name, data)
#     with open(name, 'w') as wf:

#         if not data:
#             return None

#         meta = data[0].keys()
#         print('set_csv_data,', meta)
#         wf.write(','.join(meta) + '\n')

#         for rec in data:
#             wf.write(','.join([str(rec[key])for key in meta]) + '\n')


# def set_logging(device_code):
#     logger = logging.getLogger()
#     logger.setLevel(logging.NOTSET)

#     # formatter = logging.Formatter("%(asctime)s - %(pathname)s %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s")
#     formatter = logging.Formatter(
#         "%(asctime)s %(filename)s:%(lineno)d %(levelname)s: %(message)s")

#     log_path = os.getcwd() + '/'
#     log_name = log_path + device_code+'.log'
#     logfile = log_name
#     # print('logfile',logfile)

#     file_handle = logging.FileHandler(logfile, mode='w')
#     file_handle.setLevel(logging.NOTSET)
#     file_handle.setFormatter(formatter)
#     logger.addHandler(file_handle)

#     console_handle = logging.StreamHandler()
#     console_handle.setLevel(logging.NOTSET)
#     console_handle.setFormatter(formatter)
#     logger.addHandler(console_handle)


# def urltobase64(url):
#     # 图片保存在内存
#     response = req.get(url)
#     # 得到图片的base64编码
#     ls_f = base64.b64encode(BytesIO(response.content).read())
#     # print('ls_f:', ls_f)

#     # 将base64编码进行解码
#     # imgdata = base64.b64decode(ls_f)
#     return ls_f.decode()
